//
//  MyApplyViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/24.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper

class MyApplyViewController: BaseTableViewController {

    var dataSource = [MyApplyModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的申请"
        // Do any additional setup after loading the view.
        view.backgroundColor = CDColor.grayBackgroundColorA
        tableView.backgroundColor = CDColor.grayBackgroundColorA
        tableView.register(UINib(nibName: "MyApplyCell", bundle: nil), forCellReuseIdentifier: "MyApplyCell")
        
        tableView.reloadData()
        
        let json = ["PersionID": MyselfSingleton.shared.userInfo?.userID ?? ""].convertToJsonString()
        let paramString = "<json>"+json+"</json>"
        
        NetworkingHandle.fetchNetworkData(method: "getApplyInfo", paramString: paramString, success: {[weak self] (dict) in
            guard let detailString = dict["Detail"] as? String else {
                return
            }
            guard let modelArray = Mapper<MyApplyModel>().mapArray(JSONString: detailString) else {
                return
            }
            
            self?.dataSource = modelArray
            self?.tableView.reloadData()
            if let code = dict["ResultCode"] as? Int,code == 1 {
                if let msgString = dict["ResultMessage"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
        }) {
            print("")
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyApplyCell", for: indexPath) as! MyApplyCell
        cell.model = dataSource[indexPath.row]
        cell.changeHeightClouse = { [weak self] in
            self?.tableView.reloadData()
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = dataSource[indexPath.row]
        return CGFloat(model.cellHeight)
    }
    
}
